// pages/accident/accident.js
Date.prototype.Format = function (fmt) {
  var o = {
    "M+": this.getMonth() + 1, //月份 
    "d+": this.getDate(), //日 
    "H+": this.getHours(), //小时 
  };
  if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
  for (var k in o)
    if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
  return fmt;
}
Page({

  /**
   * 页面的初始数据
   */
  data: {
    unnormalizedValue2:'',
    companyName:'',
    companyCode:'',
    address:'',
    occurentTime:new Date().Format("yyyy-MM-dd"),
    accidentNature:'',
    briefDescription:'',
    emergencyHandle:'',
  },
  //受检单位
  showRule: function () {
    this.setData({
      isRuleTrue: true
    })
  },
  hideRule: function () {
    this.setData({
      isRuleTrue: false
    })
  },
  selThis(e) {
    this.setData({
      companyName: e.detail.label,
      companyCode: e.detail.nodeCode,
      isRuleTrue: false
    })
  },
  //获取事故地点
  address: function (e) {
    this.setData({
      address: e.detail.value
    })
  },
  //输入受检时间
  DateChange(e) {
    console.log(e);
    this.setData({
      occurentTime: e.detail.value
    })
  },
 //获取事故性质
  accidentNature: function (e) {
    this.setData({
      accidentNature: e.detail.value
    })
  },
   //获取简要经过
   briefDescription: function (e) {
    this.setData({
      briefDescription: e.detail.value
    })
  },
   //获取应急处置
   emergencyHandle: function (e) {
    this.setData({
      emergencyHandle: e.detail.value
    })
  },
  //提交表格
  submitClick: function () {
    var that=this;
     wx.request({
       url: 'http://39.98.173.131:7000/wlhse/api/v3/add_event',
       method: "POST",
       data: {
         companyName: that.data.companyName,
         companyCode: that.data.companyCode,
         address: that.data.address,
         occurentTime:that.data.occurentTime,
         accidentNature: that.data.accidentNature,
         briefDescription: that.data.briefDescription,
         emergencyHandle: that.data.emergencyHandle,
       },
       header: {
         'content-type': 'application/json' ,// 默认值
         'Authorization': wx.getStorageSync("token")//读取cookie
       },
       success: function (res) {
         console.log(res.data)
       },
     })
     wx.showToast({
       title: '提交成功',
       icon: 'success',
       duration: 2000
     })
     wx.switchTab({
      url: '../index/index',
    })
   },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var company_list = []
    var that = this;
        //获取单位列表
        wx.request({
          url: 'http://39.98.173.131:7000/wlhse/api/v3/company/tree',
          data: {},
          method: 'GET',
          header: {
            'content-type': 'application/json',
            'Authorization': wx.getStorageSync("token")//读取cookie
          },
          success: function (res) {
            company_list = res.data.data
            // console.log("测试")
            // console.log(JSON.stringify(res))
            that.setData({
              unnormalizedValue2: company_list
            });
          },
          fail: function (err) {
            //失败执行
          }
        })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})